; Read sequential file and display on console
; Author:  R. Detmer
; Date:  6/98

.386
.MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

STD_OUTPUT    EQU  -11
STD_INPUT     EQU  -10
GENERIC_READ  EQU  80000000h
OPEN_EXISTING EQU  3

GetStdHandle PROTO NEAR32 stdcall,
    nStdHandle:DWORD

ReadFile PROTO NEAR32 stdcall,
    hFile:DWORD, lpBuffer:NEAR32, nNumberOfCharsToRead:DWORD,
    lpNumberOfBytesRead:NEAR32, lpOverlapped:NEAR32

WriteFile PROTO NEAR32 stdcall,
    hFile:DWORD, lpBuffer:NEAR32, nNumberOfCharsToWrite:DWORD,
    lpNumberOfBytesWritten:NEAR32, lpOverlapped:NEAR32

CreateFileA PROTO NEAR32 stdcall,
    lpFileName:NEAR32, access:DWORD, shareMode:DWORD,
    lpSecurity:NEAR32, creation:DWORD, attributes:DWORD, copyHandle:DWORD

CloseHandle PROTO NEAR32 stdcall,
    fHandle:DWORD

.DATA

written    DWORD  ?
read       DWORD  ?
fileName   BYTE   60 DUP (?)
hStdOut    DWORD  ?
hStdIn     DWORD  ?
hFile      DWORD  ?
buffer     BYTE   64 DUP (?)
prompt     BYTE   "File name?  "

.CODE
_start:
           INVOKE GetStdHandle,       ; handle for console output
             STD_OUTPUT
           mov    hStdOut, eax

           INVOKE GetStdHandle,       ; handle for console input
             STD_INPUT
           mov    hStdIn, eax

           INVOKE WriteFile,
             hStdOut,                 ; file handle for screen
             NEAR32 PTR prompt,       ; address of prompt
             12,                      ; length of prmpt
             NEAR32 PTR written,      ; bytes written
             0                        ; overlapped mode

           INVOKE ReadFile,
             hStdIn,                  ; file handle for keyboard
             NEAR32 PTR fileName,     ; address for name
             60,                      ; maximum length
             NEAR32 PTR read,         ; bytes read
             0                        ; overlapped mode

           mov    ecx, read           ; number of bytes read
           mov    BYTE PTR fileName[ecx-2],0 ; add trailing null

           INVOKE CreateFileA,        ; open file
             NEAR32 PTR fileName,     ; file name
             GENERIC_READ,            ; access
             0,                       ; no sharing
             0,                       ; no predefined security
             OPEN_EXISTING,           ; open only if file exists
             0,                       ; no special attributes
             0                        ; no copied handle
           mov    hFile, eax          ; handle for file

readLoop:  INVOKE ReadFile,
             hFile,                   ; file handle
             NEAR32 PTR buffer,       ; address for input
             64,                      ; buffer length
             NEAR32 PTR read,         ; bytes read
             0                        ; overlapped mode

           INVOKE WriteFile,
             hStdOut,                 ; file handle for screen
             NEAR32 PTR buffer,       ; address for output
             read,                    ; write same number as read
             NEAR32 PTR written,      ; bytes written
             0                        ; overlapped mode

           cmp    read, 64            ; were 64 characters read?
           jnl    readLoop            ; continue if so

           INVOKE  CloseHandle,       ; close file handle
             hfile

           INVOKE  ExitProcess, 0     ; exit with return code 0

PUBLIC _start                         ; make entry point public
END                                   ; end of source code

